package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.OrderAddresses;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.OrderAddressesDto;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.OrderAddressesMapper;
import com.example.mall.query.OrderAddressesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.IOrderAddressesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单地址表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class OrderAddressesServiceImpl extends ServiceImpl<OrderAddressesMapper, OrderAddresses> implements IOrderAddressesService {

    @Autowired
    private OrderAddressesMapper orderAddressesMapper;

    @Override
    public PageDTO<OrderAddressesDto> queryPage(OrderAddressesQuery orderAddressesQuery) {
        if (orderAddressesQuery == null) {
            orderAddressesQuery = new OrderAddressesQuery();
        }
        Page<OrderAddresses> page = orderAddressesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OrderAddresses> cartPage = lambdaQuery()
                .like(orderAddressesQuery.getAddressId()!= null, OrderAddresses::getAddressId, orderAddressesQuery.getAddressId())
                .like(orderAddressesQuery.getAddress()!= null, OrderAddresses::getAddress, orderAddressesQuery.getAddress())
                .like(orderAddressesQuery.getOrderId()!= null, OrderAddresses::getOrderId, orderAddressesQuery.getOrderId())
                .like(orderAddressesQuery.getCity()!= null, OrderAddresses::getCity, orderAddressesQuery.getCity())
                .like(orderAddressesQuery.getPostalCode()!= null, OrderAddresses::getPostalCode, orderAddressesQuery.getPostalCode())
                .like(orderAddressesQuery.getRecipientName()!= null, OrderAddresses::getRecipientName, orderAddressesQuery.getRecipientName())
                .like(orderAddressesQuery.getRecipientPhone()!= null, OrderAddresses::getRecipientPhone, orderAddressesQuery.getRecipientPhone())
                .like(orderAddressesQuery.getState()!= null, OrderAddresses::getState, orderAddressesQuery.getState())
                .like(orderAddressesQuery.getStatus()!= null, OrderAddresses::getStatus, orderAddressesQuery.getStatus())
                .orderByDesc(OrderAddresses::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, OrderAddressesDto.class);
    }

    @Override
    public void insert(OrderAddressesDto orderAddressesDto) {
        OrderAddresses orderAddresses = new OrderAddresses();
        CopyUtils.copyProperties(orderAddressesDto, orderAddresses);
        orderAddresses.setAddressId(CodeGenerateUtils.generateUniqueId());
        orderAddresses.setCreateDate(new Date());
        orderAddresses.setUpdateDate(new Date());
        orderAddresses.setStatus(BaseConstants.EFFECTIVE_STATUS);
        orderAddressesMapper.insert(orderAddresses);
    }

    @Override
    public void update(OrderAddressesDto orderAddressesDto) {
        AssertUtil.isNotNull(orderAddressesDto.getAddressId(), "地址ID不能为空");
        OrderAddresses orderAddresses = orderAddressesMapper.selectById(orderAddressesDto.getAddressId());
        AssertUtil.isNotNull(orderAddresses, "地址不存在");
        CopyUtils.copyProperties(orderAddressesDto, orderAddresses);
        orderAddresses.setUpdateDate(new Date());
        orderAddressesMapper.updateById(orderAddresses);
    }

    @Override
    public OrderAddresses queryOrderAddressesById(Long addressId) {
        AssertUtil.isNotNull(addressId, "地址ID不能为空");
        return orderAddressesMapper.selectById(addressId);
    }

    @Override
    public List<OrderAddresses> queryOrderAddressesList() {
        return this.list();
    }

    @Override
    public void delete(Long addressId) {
        AssertUtil.isNotNull(addressId, "地址ID不能为空");
        OrderAddresses orderAddresses = orderAddressesMapper.selectById(addressId);
        AssertUtil.isNotNull(orderAddresses, "地址不存在");
            orderAddresses.setStatus(BaseConstants.LOSE_STATUS);
        orderAddresses.setUpdateDate(new Date());
        orderAddressesMapper.updateById(orderAddresses);
    }

    @Override
    public void deleteBatch(List<Long> addressIds) {
        AssertUtil.isNotNull(addressIds, "地址ID列表不能为空");
        List<OrderAddresses> orderAddresses = orderAddressesMapper.selectBatchIds(addressIds);
        AssertUtil.isNotNull(orderAddresses, "地址不存在");
        for (OrderAddresses orderAddress : orderAddresses) {
            orderAddress.setStatus(BaseConstants.LOSE_STATUS);
            orderAddress.setUpdateDate(new Date());
            orderAddressesMapper.updateById(orderAddress);
        }
    }
}
